import random
import string

import xlrd
from xlutils.copy import copy

from common.operation_excel import OperationExcel, data

filepath = './data/test_register.xls'
operation = OperationExcel(filepath)


class RegisterData(object):
    data_info = {}

    def get_random_phone(self):
        """随机生成电话"""
        for k in range(10):
            self.res = "".join(random.choice("0123456789") for i in range(6))
            return self.res

    def get_random_tel(self):
        """随机生成手机"""
        mobile_begin_seed = ['134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '158', '159', '182',
                             '183',
                             '187', '188', '130', '131', '132', '155', '156', '175', '185', '186', '133', '153', '177',
                             '180',
                             '181', '189']
        mobile_seed = string.digits
        self.tel = random.choice(mobile_begin_seed) + ''.join(random.choice(mobile_seed) for m in range(8))
        return self.tel

    def get_random_password(self):
        """随机生成密码6-16位"""
        password_seed = string.digits + string.ascii_letters  # 数字和字母的组合
        password = ''.join(random.choice(password_seed) for p in range(random.randint(6, 16)))
        return password

    def get_repassword(self):
        """获得确认密码和密码相等"""
        password = self.get_random_password()
        repassword = password
        return repassword

    def get_rand_username(self):
        """随机生成用户名"""
        first_name = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤',
                      '许',
                      '何', '吕', '施', '张', '孔', '曹', '严', '华', '金', '魏', '陶', '姜', '戚', '谢', '邹', '喻', '柏', '水', '窦',
                      '章',
                      '云', '苏', '潘', '葛', '奚', '范', '彭', '郎', '鲁', '韦', '昌', '马', '苗', '凤', '花', '方', '俞', '任', '袁',
                      '柳',
                      '酆', '鲍', '史', '唐', '费', '廉', '岑', '薛', '雷', '贺', '倪', '汤', '滕', '殷', '罗', '毕', '郝', '邬', '安',
                      '常',
                      '乐', '于', '时', '傅', '皮', '卞', '齐', '康', '伍', '余', '元', '卜', '顾', '孟', '平', '黄', '和', '穆', '萧',
                      '尹',
                      '姚', '邵', '堪', '汪', '祁', '毛', '禹', '狄', '米', '贝', '明', '臧', '计', '伏', '成', '戴', '谈', '宋', '茅',
                      '庞',
                      '熊', '纪', '舒', '屈', '项', '祝', '董', '梁']

        lasts_name = ['兮陌', '颜汐', '诺菲', '蝶恋', '浮华', '繁花', '秋雨', '落霜', '轨忆', '末白', '梦徒', '往非', '朽命', '弃怀', '妆解',
                      '念她', '拾心', '如梦', '南城', '浮伤', '淡忆', '束心', '填谎', '默忆', '逆缘', '四忌', '望晴', '年轮', '若风', '凉生', '空岛',
                      '半夏', '疏离', '私念', '负约', '旧颜', '沉世', '素颜', '静谧', '薄姬', '忆白', '留井', '蔷薇', '忧深', '风掠', '萧兮', '独醉',
                      '黎铁', '擒拿', '静候', '一线', '差池', '借口', '没有', '方向', '梨落', '瑾色', '呢喃', '琴瑟', '残舞', '離殇', '陌颜', '囬首',
                      '肆意', '弄裳', '暗花', '雁芙', '灵瞳', '从筠', '细纱', '风惠']
        username = random.choice(first_name) + random.choice(lasts_name)
        return username

    def RandomEmail(self, emailType=None, rang=None):
        """随机生成邮箱"""
        emailtype = ["@qq.com", "@163.com", "@126.com"]
        # 如果没有指定邮箱类型，默认在 __emailtype中随机一个
        if emailType == None:
            randomEmail = random.choice(emailtype)
        else:
            randomEmail = emailType
        # 如果没有指定邮箱长度，默认在4-12之间随机
        if rang == None:
            rang = random.randint(4, 12)
        else:
            rang = int(rang)
        Number = "0123456789qbcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPWRSTUVWXYZ"
        randomNumber = "".join(random.choice(Number) for i in range(rang))
        email = randomNumber + randomEmail
        return email

    def get_question(self, ):
        """随机选择密码问题"""
        question = random.randint(1, 9)
        return question

    def get_answer(self, ):
        """随机生成答案"""
        answer_list = ['硝烟漫说己随风', '战线何曾旯旮空', '纵使孤身守高地', '不成仁处也英雄',
                       '炎炎盛夏荔枝娇', '喜果丰收四海飘', '昔望唐时妃子宠', '而今百姓尽逍遥',
                       '庭前栀子自天来', '暗把幽香玉瓣裁', '最是凉丛芳榭处', '依依顾盼忍离开',
                       '流水浮云者去多', '于今剩得有如何', '每每中宵清自守', '只同明月一厮磨',
                       '每忆流年总感伤', '同窗情谊最难忘', '万千往事随风去', '执手唯嗟鬓已霜',
                       '栀子花和通明大诗兄', '浓香满院任风来', '绿叶琼花一色裁',
                       '倩影婷婷犹带笑', '千枝万朵向君开'
                       ]
        answer = random.choice(answer_list)
        return answer

    def write_info_question(self):
        self.data_info["username"] = self.get_rand_username()
        self.data_info["email"] = self.RandomEmail()
        self.data_info["password"] = self.get_random_password()
        self.data_info["repassword"] = self.data_info["password"]
        self.data_info["tel"] = self.get_random_tel()
        self.data_info["question"] = self.get_question()
        self.data_info["answer"] = self.get_answer()
        # data.append(data_info)
        return self.data_info

    def write_info(self, question=0, answer=None):
        """不选择密码问题"""
        self.data_info["username"] = self.get_rand_username()
        self.check_username(self.data_info['username'])
        self.data_info["email"] = self.RandomEmail()
        self.check_email(self.data_info['email'])
        self.data_info["password"] = self.get_random_password()
        self.data_info["repassword"] = self.data_info["password"]
        self.data_info["tel"] = self.get_random_tel()
        self.data_info["question"] = question
        self.data_info["answer"] = answer
        # data.append(data_info)
        return self.data_info

    def write(self, data_info):
        ##xlutils:修改excel
        excel = xlrd.open_workbook(filepath)  # 打开一个excel
        work_book = copy(excel)  # 拷贝一份原来的excel
        wr = work_book.get_sheet(0)  # 获取第几个sheet页，
        # # 表头
        # wr.write(0, 0, "username")
        # wr.write(0, 1, "email")
        # wr.write(0, 2, "password")
        # wr.write(0, 3, "repassword")
        # wr.write(0, 4, "tel")
        # wr.write(0, 5, "question")
        # wr.write(0, 6, "answer")
        # 新建名片的数据
        i = len(wr.rows)
        wr.write(i, 0, data_info["username"])
        wr.write(i, 1, data_info["email"])
        wr.write(i, 2, data_info["password"])
        wr.write(i, 3, data_info["repassword"])
        wr.write(i, 4, data_info["tel"])
        wr.write(i, 5, data_info["question"])
        wr.write(i, 6, data_info["answer"])
        work_book.save(filepath)

    def check_email(self, email):
        """
        验证邮箱,不能重复
        :param email:
        :return:
        """
        operation.read(filepath)
        for user in data:
            if email.strip() == user["email"]:  # 判断邮箱是否已存在
                print("邮箱已被使用!请从新输入")
                return False
        return data.clear()

    def check_username(self, username):
        """
        验证用户名,不能重复
        :param username:
        :return:
        """
        operation.read(filepath)
        for user in data:
            if username.strip() == user["username"]:  # 判断用户名是否已存在
                print("用户名已被使用!请从新输入")
                return False
        else:
            return data.clear()
